package com.ityuwen.mengyagushi.account.api;

import com.ityuwen.mengyagushi.account.service.UserAccountService;
import com.ityuwen.mengyagushi.common.constant.SystemConstant;
import com.ityuwen.mengyagushi.common.login.GuiGuLogin;
import com.ityuwen.mengyagushi.common.result.Result;
import com.ityuwen.mengyagushi.common.util.AuthContextHolder;
import com.ityuwen.mengyagushi.model.account.UserAccount;
import com.ityuwen.mengyagushi.model.account.UserAccountDetail;
import com.ityuwen.mengyagushi.vo.account.AccountLockResultVo;
import com.ityuwen.mengyagushi.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;


	@PostMapping("{userId}")
	public Result saveUserAccount(@PathVariable("userId")Long userId){
		this.userAccountService.saveUserAccount(userId);
		return Result.ok();
	}

	@GuiGuLogin
	@GetMapping("getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount(){
		Long userId = AuthContextHolder.getUserId();
		if (userId == null){
			return Result.ok(new BigDecimal(0));
		}
		UserAccount userAccount = this.userAccountService.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId).select(UserAccount::getAvailableAmount));
		if (userAccount == null){
			return Result.ok(new BigDecimal(0));
		}
		return Result.ok(userAccount.getAvailableAmount());
	}

	@GuiGuLogin
	@PostMapping("check/lock")
	public Result<AccountLockResultVo> checkLock(@RequestBody AccountLockVo accountLockVo){
		AccountLockResultVo lockResultVo = this.userAccountService.checkLock(accountLockVo);
		return Result.ok(lockResultVo);
	}

	@PostMapping("minus/{orderNo}")
	public Result minus(@PathVariable("orderNo")String orderNo){
		this.userAccountService.minus(orderNo);
		return Result.ok();
	}

	@GuiGuLogin
	@GetMapping("findUserRechargePage/{page}/{limit}")
	public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit){
		Page<UserAccountDetail> userAccountDetailPage = this.userAccountService.findUserAccountDetials(page, limit, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
		return Result.ok(userAccountDetailPage);
	}

	@GuiGuLogin
	@GetMapping("findUserConsumePage/{page}/{limit}")
	public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit){
		Page<UserAccountDetail> userAccountDetailPage = this.userAccountService.findUserAccountDetials(page, limit, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
		return Result.ok(userAccountDetailPage);
	}
}


